﻿using System;
using System.Collections.Generic;
using Socium.Domain.Documents;
using Socium.Presentation;
using Socium.Science;

namespace Socium.Documents.Sociograms {
	public class NodeViewModel : ViewModelBase {
		readonly Node _node;

		public NodeViewModel(Node node) {
			_node = node;
		}

		public string Name {
			get { return Node.ToString(); }
		}

		readonly ICollection<CounterViewModel> _counters = new LinkedList<CounterViewModel>();

		public ICollection<CounterViewModel> Counters {
			get { return _counters; }
		}

		public bool IsMatch(Node node) {
			return Node == node;
		}

		#region Layout

		double _width;
		double _height;

		public event EventHandler LayoutChanged;

		public double Left {
			get { return Node.Left; }
			set {
				Node.Left = value;
				OnPropertyChanged("Left");
				OnLayoutChanged();
			}
		}

		public double Top {
			get { return Node.Top; }
			set {
				Node.Top = value;
				OnPropertyChanged("Top");
				OnLayoutChanged();
			}
		}

		public double Width {
			get { return _width; }
			set {
				_width = value;
				OnPropertyChanged("Width");
				OnLayoutChanged();
			}
		}

		public double Height {
			get { return _height; }
			set {
				_height = value;
				OnPropertyChanged("Height");
				OnLayoutChanged();
			}
		}

		void OnLayoutChanged() {
			var handler = LayoutChanged;
			if (handler != null) {
				handler(this, EventArgs.Empty);
			}
		}

		#endregion

		#region Dragging

		DragState _drag_state = DragState.Inactive();

		bool _is_dragging;

		public bool IsDragging {
			get { return _is_dragging; }
			private set {
				_is_dragging = value;
				OnPropertyChanged("IsDragging");
			}
		}

		public Node Node {
			get { return _node; }
		}

		public void BeginDrag(double x, double y) {
			_drag_state = _drag_state.BeginDrag(new Vector(Left, Top), new Vector(x, y));
			IsDragging = true;
		}

		public void DoDrag(double x, double y) {
			var position = _drag_state.DoDrag(new Vector(x, y));

			Left = position.X;
			Top = position.Y;
		}

		public void EndDrag() {
			_drag_state = _drag_state.EndDrag();
			IsDragging = false;
		}

		#endregion
	}
}
